<?php

namespace app\api\Logic\Article;

use app\api\Logic\CommonLogic;
use app\api\model\ArticleModel;
use app\api\model\InteractThemeModel;

class ArticleLogic
{

    /**
     * 获取分类详情
     *
     * @param [type] $theme_id 分类id
     * @return void
     */
    public function getThemeDetail($theme_id)
    {
        $themeModel = new InteractThemeModel();
        $themeModel
        ->alias('a')
        ->where('a.id',$theme_id)
        ->join('interact b','a.id = b.theme_id','left')
        ->where('a.deletetime','null')
        ->field('a.id,a.title,a.content,a.image,count(b.id) as article_num')
        ->group('a.id')
        ->find();

    }


    /**
     * 获取文章列表
     *
     * @param [type] $shop_id
     * @param [type] $user_id
     * @param [type] $page
     * @param [type] $limit
     * @return void
     */
    public function getArticleList($shop_id,$user_id,$page,$limit,$type=0,$theme_id=0,$order_type=1)
    {
        $shop_arr = [0,$shop_id];
        $where = [
            'a.shop_id'=>['in',$shop_arr],
            'a.status'=>1,
            'a.deletetime' => null
        ];
        if($type){//是否有文章类别
            $tmp = is_array($type)?['in',$type]:$type;
            $where['a.type'] = $tmp;
        }
        if($theme_id){//是否有二级分类
            $where['a.c_id'] = $theme_id;
        }
        $order = '';
        switch ($order_type) { //排序类型:1=置顶排序,2=普通排序,3=首页排序
            case '1':
                $order = 'a.is_top desc,a.createtime desc,a.shop_id asc';
                break;
            case '2':
                $order = 'a.createtime desc';
                break;
            case '3':
                $order = 'a.is_top desc,a.views desc,a.like desc,a.createtime desc,a.share desc,comments desc';
                break;
        }

        $articletModel = new ArticleModel();
        $res = $articletModel->alias('a')
        ->where($where)
        ->order($order)
        ->field('a.id,a.shop_id,a.title,a.type,a.c_id,a.content,a.image,a.video,a.operation_id,a.views,a.comments,a.like,a.share,a.createtime')
        ->limit(($page-1)*$limit,$limit)
        ->select();
        return $res;
    }

    /**
     * 获取文章详情
     *
     * @param [type] $param 前端传入数据 
     * relation_id 贴子id 
     * shop_id 校区id
     * user_id 用户id
     * @return void
     */
    public function getArticleDetail($param)
    {
        $articleModel = new ArticleModel();
        $commonLogic = new CommonLogic();
        $where = [
            'status' => 1,
            'deletetime' => null,
            'id' => $param['relation_id'],
        ];
        $data = $articleModel
        ->where($where)
        ->field('id,type,shop_id,c_id,title,image,video,content,like,views,comments,share,operation_id,createtime')->find();
        if($data){
             // 浏览数+1
             $commonLogic->alreadyView($param['user_id'],$param['shop_id'],$param['relation_id'],'Article','article','views');
             // 是否已点赞
             $is_like = $commonLogic->isLike($param['user_id'],$param['shop_id'],$param['relation_id'],'Article');
             $data['is_like'] = $is_like ? 1 : 0;
        }
        // $user_type = $data['shop_id']==0?0:1;
        // //获取作者头像信息
        // $user_info = $commonLogic->getUserInfo($user_type,$data['operation_id']);
        // $data['nickname'] = $user_info['nickname'];
        // $data['avatar'] = $user_info['avatar'];
        

        return $data;

    }

    /**
     * 萌娃圈 下滑获取下一个视频
     *
     * @param [type] $param 前端参数
     * @param [type] $order_type 排序类型
     * @return void
     */
    public function video_next($param,$order_type)
    {
        $articleModel = new ArticleModel();
        $commonLogic = new CommonLogic();
        $shop_arr = [0,$param['shop_id']];
        $where = [
            'a.shop_id'=>['in',$shop_arr],
            'a.status' => 1,
            'a.type' => 4,
            'a.deletetime' => null,
        ];
        $user_id = $param['user_id'];
        switch ($order_type) { //排序类型:1=推荐排序,2=热门排序
            case '1':
                $order = 'is_view asc,a.is_top desc,a.shop_id asc,a.createtime desc';
                break;
            case '2':
                $order = 'is_view asc,a.is_top desc,a.shop_id asc,a.like desc,a.views desc,a.share desc,a.createtime desc';
                break;
        }
        $data = $articleModel
        ->alias('a')
        ->where($where)
        ->join('browse_log b',"a.id = b.relation_id and b.style = 'Article' and b.user_id='{$user_id}' ",'left')
        ->join('likes c',"a.id = c.relation_id and c.style = 'Article' and c.user_id='{$user_id}' ",'left')
        ->group('a.id')
        ->field('a.id,a.type,a.shop_id,a.c_id,a.title,a.image,a.video,a.content,a.like,a.views,a.comments,a.share,a.operation_id,a.createtime,count(distinct b.id) as is_view,count(distinct c.id) as is_like')
        ->order($order)
        ->limit($param['page']-1,1)
        ->select()->toArray();
        if($data){
            // 浏览数+1
            $commonLogic->alreadyView($param['user_id'],$param['shop_id'],$data[0]['id'],'Article','article','views');
            $data = $data[0];
        }

        return $data;
    }


}
